package org.bluedream.core.config.shiro;

import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authc.LogoutFilter;
import org.bluedream.comm.utils.EmptyUtil;
import org.bluedream.core.config.shiro.loginOrganization.LoginOrgManager;
import org.bluedream.core.config.shiro.loginOrganization.LoginUserRealm;
import org.bluedream.core.utils.UserUtil;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;


/**
 * @ClassName CustomLogoutFilter
 * @Description TODO
 * @Author Administrator
 * @Data 2022/5/19 8:58
 * @Version
 */
public class CustomLogoutFilter extends LogoutFilter {
    private LoginOrgManager loginOrgManager;

    public LoginOrgManager getLoginOrgManager() {
        return loginOrgManager;
    }

    public void setLoginOrgManager(LoginOrgManager loginOrgManager) {
        this.loginOrgManager = loginOrgManager;
    }

    /**
     * 退出登录时，从 LoginOrgManager.resources 删除对应的组织信息
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @Override
    protected boolean preHandle(ServletRequest request, ServletResponse response) throws Exception {
        Subject subject = this.getSubject(request , response);
        LoginOrgManager loginOrgManager = getLoginOrgManager();
        LoginUserRealm userRealm = (LoginUserRealm) loginOrgManager.get((String) subject.getSession().getId());
        if (UserUtil.isLoginOrg(userRealm , (String) subject.getSession().getId())){
            loginOrgManager.remove((String) subject.getSession().getId());
        }

        return super.preHandle(request, response);
    }
}